package com.mico.echart.monitor.timer;



import com.mico.echart.monitor.config.GlobalObject;
import com.mico.echart.monitor.model.TreeMaps;
import com.mico.echart.monitor.server.JvmMemorySocketServer;
import com.mico.echart.monitor.server.ResourceSocketServer;
import com.mico.echart.monitor.system.JvmMemoryInfo;
import com.mico.echart.monitor.model.Resource;
import com.sun.management.OperatingSystemMXBean;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArraySet;

@Component
@EnableScheduling
public class TimeTask {

    static Log log = LogFactory.getLog(TimeTask.class);
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
    private static OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
    private static List<String> list = new ArrayList<>();

    @Scheduled(cron = "0/6 * * * * ?") // 每分钟执行一次
    public void test() {
        CopyOnWriteArraySet<JvmMemorySocketServer> webSocketSet = JvmMemorySocketServer.getWebSocketSet();
        webSocketSet.forEach(c -> {
            List<TreeMaps> memoryTrees = JvmMemoryInfo.getUsedMemoryTrees();
            try {
                c.sendMessage(GlobalObject.writeValueAsString(memoryTrees));
            } catch (IOException e) {
                e.printStackTrace();
            }
        });

        CopyOnWriteArraySet<ResourceSocketServer> resourceSocketServer = ResourceSocketServer.getWebSocketSet();
        resourceSocketServer.forEach(c -> {
            try {
                List<Resource> resources = ResourceSocketServer.getResources();
                c.sendMessage(GlobalObject.writeValueAsString(resources));
            } catch (IOException e) {
                e.printStackTrace();
            }
        });

        list.add(UUID.randomUUID().toString());
        try {
            Thread.sleep(30000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


    }
}
